package com.xinqi.modules.file.course.courseware.service.impl;

import com.xinqi.common.core.enums.CourseContentTypeEnum;
import com.xinqi.common.core.enums.ReadStatusEnum;
import com.xinqi.modules.file.course.clazz.domain.ClassStudentEntity;
import com.xinqi.modules.file.course.clazz.service.ClassStudentService;
import com.xinqi.modules.file.course.courseware.domain.CourseInteractionStudentLogEntity;
import com.xinqi.modules.file.course.courseware.domain.CourseResourceEntity;
import com.xinqi.modules.file.course.courseware.domain.CourseResourcePartEntity;
import com.xinqi.modules.file.course.courseware.service.CourseResourcePartService;
import com.xinqi.modules.file.course.courseware.service.CourseWareSliceService;
import lombok.RequiredArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
public class CourseWareSliceServiceImpl implements CourseWareSliceService {

    private final CourseResourcePartService resourcePartService;

    private final CourseInteractionStudentLogServiceImpl studentLogService;

    private final ClassStudentService classStudentService;

    /**
     * Ali互动课件切片方法
     * @author dzy
     */
    @Override
    public boolean sliceOssFile(Long key, List<String> fileUrls) {
        // CourseResourceEntity checkEntity = resourcePartService.findResource(key);
        if (CollectionUtils.isNotEmpty(fileUrls)) {
            //更新切片信息
            this.saveResourcePart(fileUrls, key);
            return true;
        } else {
            return false;
        }
    }

    /**
     * 更新切片信息
     * @author dzy
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveResourcePart(List<String> sliceList, Long contentId) {
        List<CourseResourcePartEntity> partEntities = new ArrayList<>();
        for (int i = 0; i < sliceList.size(); i++) {
            CourseResourcePartEntity partEntity = new CourseResourcePartEntity();
            partEntity.setContentId(contentId);
            partEntity.setItemType(CourseContentTypeEnum.COURSEWARE.getCode());
            partEntity.setFileType(CourseContentTypeEnum.COURSEWARE.getCode());
            partEntity.setFileUrl(sliceList.get(i));
            partEntity.setPageNumber(i);
            partEntities.add(partEntity);
        }
        // 为课件添加切片
        if (Objects.nonNull(partEntities) && partEntities.size() > 0) {
            // 更新切片状态
            resourcePartService.updateResource(contentId);
            // 更新切片信息
            resourcePartService.deletePartsByContentId(contentId);
            resourcePartService.saveBatch(partEntities);
            // 同步学生切片学习记录
            this.addStudentPart(contentId, partEntities);
        }
    }

    private void addStudentPart(Long contentId, List<CourseResourcePartEntity> parts) {
        // 获取源数据
        CourseResourceEntity entity = resourcePartService.findResource(contentId);
        // 获取学生记录
        List<ClassStudentEntity> students = classStudentService.findListByClassId(entity.getClassId());
        List<CourseInteractionStudentLogEntity> studentLogEntities = new ArrayList<>();
        for (CourseResourcePartEntity part : parts) {
            if (students != null && students.size() > 0) {
                List<CourseInteractionStudentLogEntity> list = students.stream().map(student -> {
                    //创建切片学习对象
                    CourseInteractionStudentLogEntity studentLogEntity = new CourseInteractionStudentLogEntity();
                    studentLogEntity.setClassId(student.getClassId());
                    studentLogEntity.setContentId(contentId);
                    studentLogEntity.setResourcePartId(part.getId());
                    studentLogEntity.setType(CourseContentTypeEnum.COURSEWARE.getCode());
                    studentLogEntity.setIsRead(ReadStatusEnum.UNREAD.getCode());
                    studentLogEntity.setStudentId(student.getStudentId());
                    //更新查阅次数
                    studentLogEntity.setConsultNum(0L);
                    return studentLogEntity;
                }).collect(Collectors.toList());
                studentLogEntities.addAll(list);
            }
        }
        // 添加学生切片记录
        if (Objects.nonNull(studentLogEntities) && studentLogEntities.size() > 0) {
            studentLogService.deleteStudentLogByContentId(contentId);
            studentLogService.saveBatch(studentLogEntities);
        }
    }

}
